package org.itboys.mobile.controller.weixin;

import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dto.SundayMemberSessionDto;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.service.mongo.weixin.SundayWeiXinMemberService;
import org.itboys.mobile.util.AccountIdHoldUtils;
import org.itboys.mobile.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 作者：zzq
 * 时间：2017年5月9日
 * 描述：众包_微信_支付_controller
 */
@Controller
@RequestMapping("/zhongbao/weixin/pay")
public class ZhongBaoWeiXinPayController extends BaseController{
    @Autowired
    private SundayWeiXinMemberService memberService;


    /**
     * 支付
     * @param request
     * @param response
     */
    @RequestMapping(value="/index", method= RequestMethod.GET)
    @ApiOperation(value="支付页面")
    public String member_step1(
            HttpServletRequest request,
            HttpServletResponse response,
            Model model
            ){
        try{
            return "/zhongbao/weixin/pay/index";
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code= CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/zhongbao/weixin/pay/index----e="+e.getMessage());

            }
            e.printStackTrace();
            return "/zhongbao/wexin/error";
        }
    }
    /**
     * 支付回掉
     * @param request
     * @param response
     * @param
     */
    @RequestMapping(value="/callback", method=RequestMethod.POST)
    @ApiOperation(value="支付回掉")
    public void read(
            HttpServletRequest request,
            HttpServletResponse response  ){
        try {
            // SundayMemberSessionDto memberSessionDto = AccountIdHoldUtils.getSessionMember(request);
            SundayMember member = new SundayMember();
            member.setUserType(SundayCommonConstants.user_type_business);
           // member.setStatus(SundayCommonConstants.user_status_zc);
          //  member.setVersion(SundayCommonConstants.zhongbao_project_version_business);
            memberService.updateExceptEmpty(AccountIdHoldUtils.getSessionMember(request).getId(),member);
            memberService.insertMemberToSession(request,AccountIdHoldUtils.getSessionMember(request).getId(),null);
            ResponseMessage.success();
        } catch (Exception e) {

            String msg ="";
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
            }else{
                msg= CommonConstants.ERROMSG;
                logger.info("/zhongbao/weixin/pay/callback--------"+e);
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE,msg);
        }
    }
}
